package webSocket.TCP;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",6666);
        try (InputStream input = socket.getInputStream()) {
            try (OutputStream output = socket.getOutputStream()) {
                handle(input, output);
            }
        }
        socket.close();
        System.out.println("disconnected.");
    }

    private static void handle(InputStream input, OutputStream output) throws IOException {
        var reader = new BufferedReader(new InputStreamReader(input));
        var writer = new BufferedWriter(new OutputStreamWriter(output));
        Scanner scanner = new Scanner(System.in);
        System.out.println("[server] "+reader.readLine());
        for(;;){
            System.out.println(">>> ");
            String command = scanner.nextLine();
            writer.write(command);
            writer.newLine();
            writer.flush();
            String response = reader.readLine();
            System.out.println("<<< "+response);
            if(response.equals("bye")){
                break;
            }
        }
    }
}
